﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Edm;
using Obsidian.Data.Sql;
using Oak.Model;

namespace Oak.Core.Data
{
    public class ActionDA
    {

        internal List<CoActionInfo> SelectList()
        {
            CoActionInfo a = new CoActionInfo();
            IDbHandler dbh = a.CreateHandler();
            dbh.SetFields(a.Fields);
            DataReader dr = dbh.Select();
            List<CoActionInfo> list = new List<CoActionInfo>();
            while (dr.Read())
            {
                CoActionInfo act = new CoActionInfo();
                dr.ReadTo(act);
                list.Add(act);
            }
            dr.Close();
            return list;
        }

        internal List<CoActionInfo> SelectList(CoActionQry qry, out ListAttrInfo listAttr)
        {

            CoActionInfo a = new CoActionInfo();
            CoActCategoryInfo c = new CoActCategoryInfo();
            IDbHandler dbh = a.CreateHandler();
            dbh.SetFields(a.Fields);
            dbh.SetFields(c.Fields);

            dbh.Join(c).On(a.ActionCatId.Equals(c.CategoryId));

            if (!qry.Keyword.IsNullOrEmpty)
            {
                CompoundCondition cc = new CompoundCondition();
                cc.Or(a.ActionName.Like(qry.Keyword.Value))
                    .Or(a.Description.Like(qry.Keyword.Value));
                dbh.Where(cc);
            }

            dbh.OrderBy(a.ActionCatId, OrderType.ASC);
            dbh.OrderBy(a.ActionName, OrderType.ASC);

            DataReader dr = dbh.Select(qry, out listAttr);
            List<CoActionInfo> list = new List<CoActionInfo>();
            while (dr.Read())
            {
                CoActionInfo act = new CoActionInfo();
                dr.ReadTo(act, act.ActCategory.New());
                list.Add(act);
            }
            dr.Close();
            return list;

        }

    }
}
